#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include<iostream>
#include<vector>
using namespace std;
class String {
protected:
	char* mystr;
	int len;
public:
	String(const char* p) {
		len = strlen(p);
		mystr = new char[len + 1];
		strcpy(mystr, p);
	}
	~String() {
		if (mystr != NULL) {
			delete[]mystr;
			mystr = NULL;
			len = 0;
		}
	}
	void showStr() {
		cout << mystr;
	}
	char* GetStr() {
		return mystr;
	}
	virtual bool IsSubString(const char* str) {
		int i, j;
		for (i = 0; i < len; i++) {
			int k = i;
			for (j = 0; str[j] != '\0'; j++, k++) {
				if (str[j] != mystr[k]) break;
			}
			if (str[j] == '\0') return true;
		}
		return false;
	}
};

class EditString :public String {
public:
	EditString(const char* p) :String(p) {}
	int IsSubString(int start, const char* str);
	void EditChar(char s, char d);
	void EditSub(char* subs, char* subd);
	void DeleteChar(char ch);
	void DeleteSub(char* sub);
};

int EditString::IsSubString(int start, const char* str)
{
	if (start<-1 || start >= len)
		return -1;
	int i, j;
	for (i = start; i < len; i++) {
		int k = i;
		for (j = 0; str[j] != '\0'; j++, k++) {
			if (str[j] != mystr[k]) break;
		}
		if (str[j] == '\0') return i;
	}
	return -1;
}

void EditString::EditChar(char s, char d)
{
	for (int i = 0; i < len; i++)
	{
		if (mystr[i] == s) mystr[i] = d;
	}
}

void EditString::EditSub(char* subs, char* subd)
{
	vector<int> position;
	int tmp = 0;
	while (tmp <= len)
	{
		int tmpp = IsSubString(tmp, subs);
		if (tmpp == -1)break;
		position.push_back(tmpp);
		tmp = position.back() + 1;
	}
	if (position.size() == 0)return;//把所有的位置放在position里面
	int new_len = len + (strlen(subd) - strlen(subs)) * position.size();
	char* new_mystr = new char[new_len + 1];
	int k = 0;
	for (int i = 0, j = 0; i < len; i++, j++)
	{
		if ( k < position.size()&& i == position[k])
		{
			new_mystr[j] = '\0';
			strcat(new_mystr, subd);
			i += strlen(subs) - 1;
			j += strlen(subd) - 1;
			k++;
		}
		else
		{
			new_mystr[j] = mystr[i];
		}
	}
	new_mystr[new_len] = '\0';
	delete[]mystr;
	mystr = new_mystr;
	len = new_len;
}

void EditString::DeleteChar(char ch)
{
	vector<char> vchar;
	for (int i = 0; i < this->len; i++)
	{
		if (mystr[i] != ch)vchar.push_back(mystr[i]);
	}
	int newlen = vchar.size();
	char* mynewstr = new char[newlen + 1];
	int i = 0;
	for (; i < newlen; i++)
	{
		mynewstr[i] = vchar[i];
	}
	delete[]mystr;
	mynewstr[newlen] = '\0';
	mystr = mynewstr;
	len = newlen;
}


void EditString::DeleteSub(char* subs)
{
	int len_sub = strlen(subs);
	vector<int> position;
	int tmp = 0;
	while (tmp <= len)
	{
		int tmpp = IsSubString(tmp, subs);
		if (tmpp == -1)break;
		position.push_back(tmpp);
		tmp = position.back() + 1;
	}
	if (position.size() == 0)return;//把所有的位置放在position里面
	int new_len = len - strlen(subs) * position.size();
	char* new_mystr = new char[new_len + 1];
	int k = 0,j = 0;//position
	for (int i = 0; mystr[i] != '\0'; i++, j++)
	{
		if (k < position.size() && i == position[k])
		{
			i += len_sub - 1;
			j -= 1;
			k += 1;
		}
		else
		{
			new_mystr[j] = mystr[i];
		}
	}
	new_mystr[j] = '\0';
	delete[]mystr;
	mystr = new_mystr;
	len = new_len;
}
